package cn.sfa.common.quartzJob.AccountJob;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import microsoft.exchange.webservices.data.Appointment;
import microsoft.exchange.webservices.data.AppointmentType;
import microsoft.exchange.webservices.data.BodyType;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.SendInvitationsMode;
import microsoft.exchange.webservices.data.StringList;
import microsoft.exchange.webservices.data.TimeZoneDefinition;
import microsoft.exchange.webservices.data.WebCredentials;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import cn.sfa.common.quartzJob.QuartzMessageService;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.ConfigUtil;
import cn.sfa.entity.quartz.SaleQuartzMessage;
import cn.sfa.entity.routine.SaleMessagestake;
import cn.sfa.entity.train.SaleTrainer;
import cn.sfa.service.routine.CalendarService;
import cn.sfa.service.routine.ExChangeCalendarService;

/**
 * 
 * 类名称：AccountService 类描述：代理人帐号同步守护进程 创建人：lenovo 创建时间：2012-2-14 下午9:40:42 修改备注：
 * 
 * @version v1.0
 */
public class AccountService {

	private NamedParameterJdbcTemplate jdbcTemplate;

	public NamedParameterJdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(NamedParameterJdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	Logger log = LoggerFactory.getLogger(AccountService.class);

	/*
	 * 获取未同步到SFA中的FAST代理人帐号
	 */
	public List<AccountEntity> getUnSynchAccount() {
		StringBuffer sql = new StringBuffer();
		/*
		 * sql.append(" select ab.agentid, "); sql.append("ab.agentcode, ");
		 * sql.append("ab.agentname, "); sql.append("ab.branchid, ");
		 * sql.append("cu.idno, "); sql.append("cu.contactmobile ");
		 * sql.append("from agentbroker ab ");
		 * sql.append("left join customer cu ");
		 * sql.append("on ab.customerid = cu.customerid ");sql.append(
		 * "left join (select * from saleuserslogin where userstatus = 'y') su "
		 * ); sql.append("on su.useragentid = ab.agentid ");
		 * sql.append("where su.saleusersloginid is null ");
		 * sql.append(" and ab.agentstatus in ('AGENTSTATUS_09') ");sql.append(
		 * " start with ab.agentid = 'AGEF000000000151' connect by prior ab.agentid=ab.manageragentid "
		 * );
		 */
		// sql.append("and rownum <= 200 ");

		sql.append(" select ab.agentid, ");
		sql.append(" ab.agentcode, ");
		sql.append(" ab.agentname, ");
		sql.append(" ab.branchid, ");
		sql.append(" (select cu.idno from customer cu  where ab.customerid = cu.customerid) idno, ");
		sql.append(" (select cu.contactmobile from customer cu  where ab.customerid = cu.customerid)  contactmobile ");
		sql.append(" from agentbroker ab   ");
		sql.append(" where 1=1   ");
		sql.append(" and not exists(select * from saleuserslogin su where userstatus = 'y' and ab.agentid= su.useragentid) ");
		sql.append(" and ab.agentstatus in ('AGENTSTATUS_09')  ");
	/*	sql.append(" start with ab.agentid = 'AGEF000000000151' ");
		sql.append(" connect by nocycle prior ab.agentid = ab.manageragentid ");*/
		Map<String, Object> paramMap = new HashMap<String, Object>();
		return jdbcTemplate.query(sql.toString(), paramMap,
				new BeanPropertyRowMapper<AccountEntity>(AccountEntity.class));
	}

	/**
	 * 获取FAST 已经辞职的人员 但未更新到SFA
	 * 
	 * @return
	 */
	public List<AccountEntity> getUnSynchResign() {
		StringBuffer sql = new StringBuffer();

		sql
				.append(" select agentname,agentcode,agentid,sa.branchid,idno,sa.contactmobile from saleuserslogin sa ");
		sql
				.append(" left join (select  ab.agentid,ab.agentcode,ab.agentname,ab.branchid,ab.agentstatus,cu.idno,cu.contactmobile");
		sql
				.append(" from agentbroker ab left join customer cu on ab.customerid = cu.customerid  ) su on sa.useragentid = su.agentid");
		sql
				.append(" where sa.userstatus='y' and agentstatus in ('AGENTSTATUS_10')");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		return jdbcTemplate.query(sql.toString(), paramMap,
				new BeanPropertyRowMapper<AccountEntity>(AccountEntity.class));
	}

	/*
	 * 根据代理人的身份证号生成密码
	 */
	public String createPWD(String idno) {
		String basePwd = "hkfs@2006";
		String baseNo = "000000";
		int length = idno.length();
		if (length == 15 || length == 18) {
			baseNo = idno.substring(length - 6);
		}
		return basePwd + baseNo;
	}

	/**
	 * 插入数据
	 */

	public boolean insertAccount(String saleuserlonginid,// 记录ID
			String useragentid, // 代理人ID
			String agentname, // 代理人姓名
			String branchid, // 代理人机构ID
			String contactmobile, // 代理人手机号
			String idno, // 代理人手机号
			String loginname // 

	) {
		String userStatus = "y";
		String userTpye = "USERTYPE_1";
		String userpwd = createPWD(idno);
		String sqlStr = " insert into saleuserslogin(SALEUSERSLOGINID,USERAGENTID,LOGINNAME,USERNAME,BRANCHID,CONTACTMOBILE,USERSTATUS,USERPASSWORD,USERTYPE)values"
				+ "(:SALEUSERSLOGINID,:USERAGENTID,:LOGINNAME,:USERNAME,:BRANCHID,:CONTACTMOBILE,:USERSTATUS,:USERPASSWORD,:USERTYPE)";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("SALEUSERSLOGINID", saleuserlonginid);
		paramMap.put("USERAGENTID", useragentid);
		paramMap.put("BRANCHID", branchid);
		paramMap.put("USERNAME", agentname);
		paramMap.put("CONTACTMOBILE", contactmobile);
		paramMap.put("USERSTATUS", userStatus);
		paramMap.put("USERPASSWORD", userpwd);
		paramMap.put("LOGINNAME", loginname);
		paramMap.put("USERTYPE", userTpye);
		int flag = jdbcTemplate.update(sqlStr, paramMap);
		if (flag > 0) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 更新数据
	 * 
	 */
	public boolean updateAccount(String useragentid) {
		String userStatus = "y";
		String sqlStr = " update saleuserslogin set USERSTATUS = :userStatus where USERAGENTID = :useragentid";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("useragentid", useragentid);
		paramMap.put("userStatus", userStatus);
		boolean result = jdbcTemplate.update(sqlStr, paramMap) > 0;
		return result;
	}

	/**
	 * 更新数据
	 * 
	 */
	public boolean StopAccount(String useragentid) {
		String userStatus = "N";
		String sqlStr = " update saleuserslogin set USERSTATUS = :userStatus where USERAGENTID = :useragentid";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("useragentid", useragentid);
		paramMap.put("userStatus", userStatus);
		boolean result = jdbcTemplate.update(sqlStr, paramMap) > 0;
		return result;
	}

	/**
	 *处理丢失的数据
	 * 
	 */
	public List<AccountEntity> lostAccount() {
		String sqlStr = " select loginname from saleuserslogin ";
		// List<String> lostaccount;
		// lostaccount = jdbcTemplate.queryForInt(sqlStr, paramMap);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		return jdbcTemplate.query(sqlStr.toString(), paramMap,
				new BeanPropertyRowMapper<AccountEntity>(AccountEntity.class));
	}
	/**
	 *查询数据
	 * 
	 */
	public boolean seachAccount(String agentname) {
		String sqlStr = " select loginname from saleuserslogin where loginname = :agentname ";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		boolean result = jdbcTemplate.update(sqlStr, paramMap) > 0;
		return result;
	}

}
